package ru.vt.simpleservlet.dbinterraction;

import ru.vt.simpleservlet.data.DataPosition;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Vit
 */
public class Inserter {

    public static void addData(DataPosition dataPosition) {
        try {
            Class.forName(Settings.getDriverName());
            String url = "jdbc:mysql://" + Settings.getServerName() + ":" + Settings.getServerPort() + "/" + Settings.getMydatabase();
            Connection connection = (Connection) DriverManager.getConnection(url, Settings.getUsername(), Settings.getPassword());
            Statement statement = (Statement) connection.createStatement();
            String quo = "'";
            String com=",";
            String insert="INSERT INTO positionlist(versioncode,categoryname,name,textcontent,price,image) VALUES ("+quo+dataPosition.getVersionCode()+quo+com+quo+dataPosition.getCategoryName()+quo+com+quo+dataPosition.getName()+quo+com+quo+dataPosition.getTextContent()+quo+com+dataPosition.getPrice()+com+quo+dataPosition.getImage()+quo+")";
            System.out.println(insert);
            statement.executeUpdate(insert);
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
